package com.zzh.wxdemo.utils;

import android.text.TextUtils;
import android.util.Log;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class FileUtils {

    /**
     * 复制单个文件
     *
     * @param oldPath String 原文件路径 如：c:/fqf.txt
     * @param newPath String 复制后路径 如：f:/fqf.txt
     * @return boolean
     */

    public static boolean copyFile(String oldPath, String newPath) {
        deleteFolderFile(newPath, true);
        Log.e("copyFile", "time_1:"+ System.currentTimeMillis());
        InputStream inStream = null;
        FileOutputStream fs = null;
        try{
            int bytesum = 0;
            int byteread = 0;
            File oldfile = new File(oldPath);
            Boolean flag = oldfile.exists();
            Log.e("copyFile", "flag:"+flag );
            if (flag) {
                Log.i("zzh", ">>>>>>1");
                if (oldfile.isDirectory()) {
                    Log.i("zzh", ">>>>>>2");
                    String fileStr[] = oldfile.list();
                    Log.i("zzh", ">>>>>>2" + fileStr);
                    File filesp[] = oldfile.listFiles();
                    if (filesp != null && filesp.length > 0) {
                        Log.i("zzh", ">>>>>>3");
                        for (int i = 0; i < filesp.length; i++) {
                            Log.i("zzh", ">>>>>>4");
                            Log.i("zzh", filesp[i].getName());
                        }
                    }
                }
            }
//            if(oldfile.exists()) { //文件存在时
//                inStream = new FileInputStream(oldPath); //读入原文件
//                fs = new FileOutputStream(newPath);
//                byte[] buffer = new byte[2048];
//                while((byteread = inStream.read(buffer)) != -1) {
//                    bytesum += byteread; //字节数 文件大小
//                    fs.write(buffer, 0, byteread);
//                }
//                Log.e("copyFile", "time_2:"+ System.currentTimeMillis());
//            }
        } catch(Exception e) {
            System.out.println("复制单个文件操作出错");
            e.printStackTrace();
        } finally{
            try{
                if(inStream != null) {
                    inStream.close();
                }
                if(fs != null) {
                    fs.close();
                }
            } catch(IOException e) {
                e.printStackTrace();
            }
        }
        return true;
    }

    /**
     * 删除单个文件
     *
     * @param filepath
     * @param deleteThisPath
     */

    public static void deleteFolderFile(String filepath, boolean deleteThisPath) {
        if(!TextUtils.isEmpty(filepath)) {
            try{
                File file = new File(filepath);
                if(file.isDirectory()) {
                    //处理目录
                    File files[] = file.listFiles();
                    for(int i = 0; i < file.length(); i++) {
                        deleteFolderFile(files[i].getAbsolutePath(), true);
                    }
                }
                if(deleteThisPath) {
                    if(!file.isDirectory()) {
                        //删除文件
                        file.delete();
                    } else{
                        //删除目录
                        if(file.listFiles().length == 0) {
                            file.delete();
                        }
                    }
                }
            } catch(Exception e) {
                e.printStackTrace();
            }
        }
    }
}
